package club.babyf.springai.config;

import club.babyf.springai.dao.UserRepository;
import cn.dev33.satoken.error.SaErrorCode;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static cn.dev33.satoken.exception.NotLoginException.INVALID_TOKEN;
import static cn.dev33.satoken.exception.NotLoginException.INVALID_TOKEN_MESSAGE;

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(SecurityProperties.class)
public class WebConfig implements WebMvcConfigurer {

    private final UserRepository userRepository;

    @Primary
    @Bean(name = "asyncTaskExecutor")
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 根据需求调整
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setThreadNamePrefix("MyApp-Async-");
        executor.initialize();
        return executor;
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(asyncTaskExecutor());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(
                        new SaInterceptor(r -> {
                            StpUtil.checkLogin();
                            Object loginId = StpUtil.getLoginId();
                            userRepository.findById(loginId.toString())
                                    .orElseThrow(
                                            () -> NotLoginException.newInstance(
                                                    StpUtil.getLoginType(),
                                                    INVALID_TOKEN,
                                                    INVALID_TOKEN_MESSAGE,
                                                    StpUtil.getTokenValue()
                                            ).setCode(SaErrorCode.CODE_11012)
                                    );
                        })
                )
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/spring-ai/user/register",
                        "/spring-ai/user/login",
                        "/ts.zip",
                        "/openapi",
                        "/openapi-ui",
                        "/favicon.ico"
                );
    }

}
